package com.xtrip.adm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tcl.platform.database.query.SimpleQuery;
import com.xtrip.adm.model.CBD;
import com.xtrip.adm.model.City;
import com.xtrip.adm.model.Province;
import com.xtrip.adm.service.CityService;
import com.xtrip.adm.stat.data.TcHotelGeo;
import com.xtrip.base.BaseController;
import com.xtrip.common.BusinessConstant;
import com.xtrip.net.response.ResponseDataParser;
import com.xtrip.net.util.RequestSend;
import com.xtrip.util.CharConverter;
import com.xtrip.util.parse.xml.CtripParser;
import com.xtrip.util.parse.xml.XtripDocumentHelper;

@Controller("admin")
@RequestMapping("admin")
public class AdminController extends BaseController{

	@Resource(name = "cityService")
	private CityService	cityService;

	@RequestMapping("/index")
	public String index(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		System.out.println("Hello this is adminController");
		return "/adm/index";
	}

	@RequestMapping("/addZhunaCity")
	public void addZhunaCity(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		System.out.println("Hello this is addCity");
		Map<String,String> prov = cityService.getProvinceMap();
		SimpleQuery sq = new SimpleQuery(City.class);
		List<City> l = sq.list();
		Map<String,String> params = new HashMap<String,String>();
		params.put("method", "city");
		params.put("agent_id", "3279544");
		params.put("agent_md", "e442f724b7c8c5d8");
		String result = new RequestSend(BusinessConstant.REQUEST_URL_ZHUNA,
				BusinessConstant.TYPE_HOTEL,
				BusinessConstant.PROVIDER_HOTEL_ZHUNA, params).post();
		List<City> list = (List<City>) ResponseDataParser.parse(
				BusinessConstant.TYPE_HOTEL,
				BusinessConstant.PROVIDER_HOTEL_ZHUNA, result);
		int succCnt = 0;
		int failCnt = 0;
		int k = 0;
		for(City tmp:list){
			System.out.println("正在导入第"+k+"条数据");
			boolean succ = false;
			for(City city:l){
				String cname = tmp.getName();
				String cid = tmp.getZhunaCid();
				int i = -1;
				if((i=cname.indexOf("（"))!=-1){
					cname = cname.substring(0, i);
				}
				if(cname.length()>2 && cname.substring(cname.length()-1).equals("市")){
					cname = cname.substring(0, cname.length()-1);
				}
				if(cname.length()>2 && cname.substring(cname.length()-1).equals("县")){
					cname = cname.substring(0, cname.length()-1);
				}
				if(cname.equals(city.getName())){
					city.setZhunaCid(cid);
					city.setZhunaHCnt(tmp.getZhunaHCnt());
					succ = cityService.update(city);
					succCnt++;
				}
			}
			if(!succ){
				System.out.println("第"+k+"条数据，导入失败！");
				System.out.println("name:"+tmp.getName()+",zhunaCid:"+tmp.getZhunaCid()+",provId:"+prov.get(tmp.getProId())+",zhunacnt:"+tmp.getZhunaHCnt());
				failCnt ++;
			}else{
				System.out.println("第"+k+"条数据，导入成功！");
			}
			k++;
		}
		JSONObject jo = new JSONObject();
		jo.put("succCnt", succCnt);
		jo.put("failCnt", failCnt);
		out(response, jo);
	}
	@RequestMapping("/addZhunaHotel")
	public void addZhunaHotel(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		System.out.println("Hello this is addCity");
		
		Map<String,String> params = new HashMap<String,String>();
		params.put("method", "city");
		params.put("agent_id", "3279544");
		params.put("agent_md", "e442f724b7c8c5d8");
		String result = new RequestSend(BusinessConstant.REQUEST_URL_ZHUNA,
				BusinessConstant.TYPE_HOTEL,
				BusinessConstant.PROVIDER_HOTEL_ZHUNA, params).post();
		List<City> list = (List<City>) ResponseDataParser.parse(
				BusinessConstant.TYPE_HOTEL,
				BusinessConstant.PROVIDER_HOTEL_ZHUNA, result);
		int succCnt = 0;
		int failCnt = 0;
		for(City city:list){
			boolean succ = cityService.insertCity(city);
			if(succ)succCnt++;
			else failCnt++;
		}
		JSONObject jo = new JSONObject();
		jo.put("succCnt", succCnt);
		jo.put("failCnt", failCnt);
		out(response, jo);
	}
	@RequestMapping("/updateZhunaHotel")
	public void updateZhunaHotel(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		System.out.println("Hello this is addCity");
		
		Map<String,String> params = new HashMap<String,String>();
		params.put("method", "city");
		params.put("agent_id", "3279544");
		params.put("agent_md", "e442f724b7c8c5d8");
		String result = new RequestSend(BusinessConstant.REQUEST_URL_ZHUNA,
				BusinessConstant.TYPE_HOTEL,
				BusinessConstant.PROVIDER_HOTEL_ZHUNA, params).post();
		List<City> list = (List<City>) ResponseDataParser.parse(
				BusinessConstant.TYPE_HOTEL,
				BusinessConstant.PROVIDER_HOTEL_ZHUNA, result);
		int succCnt = 0;
		int failCnt = 0;
		for(City city:list){
			boolean succ = cityService.insertCity(city);
			if(succ)succCnt++;
			else failCnt++;
		}
		JSONObject jo = new JSONObject();
		jo.put("succCnt", succCnt);
		jo.put("failCnt", failCnt);
		out(response, jo);
	}
	@RequestMapping("/addCtripCity")
	public void addCtripCity(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		System.out.println("Hello this is addCtripCity");
		
		Document doc = null;
		try {
			doc = XtripDocumentHelper.readFile("E:\\nexttrips\\ctrip\\API2.0相关静态文件\\国内城市.xml");
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String itemName = "CityDetail";
		List<City> list = CtripParser.parseCtripCity(doc, itemName);
		int succCnt = 0;
		int failCnt = 0;
		int tot = list.size();
		for(City city:list){
			String name = city.getName();
			String pinyin = CharConverter.getPingYin(name);
			String suoxie = CharConverter.getPinYinHeadChar(name);
			String abcd = suoxie.substring(0, 1);
			city.setAbcd(abcd);
			city.setPinyin(pinyin);
			city.setSuoxie(suoxie);
			boolean succ = cityService.insertCity(city);
			if(succ)succCnt++;
			else failCnt++;
		}
		JSONObject jo = new JSONObject();
		jo.put("succCnt", succCnt);
		jo.put("failCnt", failCnt);
		jo.put("tot", tot);
		out(response, jo);
	}
	@RequestMapping("/addTcCity")
	public void addTcCity(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		System.out.println("Hello this is addTcCity");
		
		TcHotelGeo thg = new TcHotelGeo();
		thg.parse("E:\\nexttrips\\17uu\\静态数据文件\\hotel-goe-cn.xml");
		List<Province> pList = thg.getpList();
		List<City> cList = thg.getcList();
		List<CBD> cbdList = thg.getCbdList();
		int csuccCnt = 0;
		int cfailCnt = 0;
		int ctot = cList.size();
		int cbdtot = cbdList.size();
		for(City city:cList){
			boolean succ = cityService.insertCity(city);
			if(succ)csuccCnt++;
			else cfailCnt++;
		}
		for(CBD cbd:cbdList){
			boolean succ = cbdService.insertCBD(cbd);
			if(succ)csuccCnt++;
			else cfailCnt++;
		}
		JSONObject jo = new JSONObject();
		jo.put("csuccCnt", csuccCnt);
		jo.put("cfailCnt", cfailCnt);
		jo.put("ctot", ctot);
		jo.put("cbdtot", cbdtot);
		out(response, jo);
	}
}
